package com.yunyike.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class LocationUtils {

	private static final Logger logger = LoggerFactory.getLogger(LocationUtils.class);

	@Value("${countries}")
	public String countries;
	
	public String getCountryByLatitudeAndLongitude(String LatitudeAndLongitude){
		
		String urlStr = "http://maps.google.cn/maps/api/geocode/json?latlng="+LatitudeAndLongitude+"&language=CN";  
		
		// 创建HttpClient实例     
        HttpClient httpclient = new DefaultHttpClient();  
        // 创建Get方法实例     
        HttpGet httpgets = new HttpGet(urlStr);
		
        logger.info("根据经纬度获取城市地址: " + urlStr);
        try {
			HttpResponse response = httpclient.execute(httpgets);    
			HttpEntity entity = response.getEntity(); 
			
			if (entity != null) {
				String strCon = EntityUtils.toString(entity);
				logger.info("Response content: " + strCon);
				
				EntityUtils.consume(entity);
				
				logger.info("当前配置的国家："+countries);
				String[] split = countries.split(",");
				for (String string : split) {
					if(strCon.indexOf(string) != -1){
						
						logger.info("当前位置支持购物："+string);
						
						return string;
					}
				}
			    
			    return null;
			    
			}
		} catch (Exception e) {
			logger.error("根据经纬度获取城市失败"+ e,e.getMessage());
		}
		
		return null;
	}
	
}
